package com.je.app.persistence;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import com.je.app.model.Observacion;
import com.je.app.model.Sesion;
import com.je.app.model.Usuario;

public class DaoSesion extends DaoGeneric {

	private static DaoSesion instance = new DaoSesion();

	public static DaoSesion getInstance() {

		return instance;
	}

	private DaoSesion() {

	}

	public Usuario validate(String idUsuario, String contrasenia){
			
		String query = "FROM Usuario WHERE id_usuario = '" + idUsuario + "' and contrasenia = '" + contrasenia + "'";
		
		Session session = HibernateUtil.getSession();
		session.beginTransaction();
        List list = session.createQuery(query).list();
        session.getTransaction().commit();
        if(list.size()!=1){
        	return null;
        }
        else{
        	return (Usuario) list.get(0);
        }
        
        



	}

	public double cajaUltimaSesion() {
		
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List list = session.createQuery("FROM Sesion WHERE idSesion = (SELECT MAX(s.idSesion) FROM Sesion s)").list();
        session.getTransaction().commit();
        Sesion sesion = (Sesion) list.get(0);

		return sesion.getCajaFinal();
		
		
	}
	
	public List <Sesion> sesionesAbiertas(){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        List list = session.createQuery("FROM Sesion WHERE horaFin is null").list();
        session.getTransaction().commit();
        return list;
	}
	
	public List list(){
		
    	Session session = HibernateUtil.getSession();
        session.beginTransaction();
        List list = session.createQuery("FROM Sesion s WHERE s.horaFin != null ORDER BY s.idSesion DESC").setMaxResults(50).list();
        session.getTransaction().commit();
        
        return list;
	}

	public List<Sesion> filter(Date fecha, String idUsuario) {
		String query = "";
		
		if(fecha!=null){
			java.sql.Date inferior = new java.sql.Date(fecha.getTime());
			fecha.setDate(fecha.getDate()+1);
			java.sql.Date superior = new java.sql.Date(fecha.getTime());
			query = query + " and ( ( s.horaInicio >= '" + inferior + "' and s.horaInicio <='" + superior + "' ) or " ;
			query = query + " ( s.horaFin >= '" + inferior + "' and s.horaFin <='" + superior + "' ) ) " ;
		}
    	
		if(idUsuario!=null){
			query = query + " and s.usuario.idUsuario = '"+idUsuario+"' ";
		}
		Session session = HibernateUtil.getSession();
        session.beginTransaction();
        List<Sesion> list = session.createQuery("FROM Sesion s WHERE s.horaFin != null " + query + " ORDER BY s.idSesion DESC").setMaxResults(150).list();
        session.getTransaction().commit();
        
        return list;

	}

	public boolean validarNombreApellidoUnico(String nombre, String apellido, String id) {
		
		Session session = HibernateUtil.getSession();
        session.beginTransaction();
        List<Sesion> list = session.createQuery("FROM Usuario u WHERE u.idUsuario!= '"+id+"' and u.nombre='"+nombre+"' and u.apellido='"+apellido+"'").list();
        session.getTransaction().commit();
        if(list.size()>0) return false;
        else return true;
	}

	public boolean validarIdUnico(String id) {
		Session session = HibernateUtil.getSession();
        session.beginTransaction();
        List<Sesion> list = session.createQuery("FROM Usuario u WHERE u.idUsuario='"+id+"'").list();
        session.getTransaction().commit();
        if(list.size()>0) return false;
        else return true;
	}

	public List<Observacion> listaObservaciones(String nombreHab) {
    	Session session = HibernateUtil.getSession();
        session.beginTransaction();
        List list = session.createQuery("FROM Observacion o WHERE o.habitacion.nombre='"+nombreHab+"' ORDER BY o.id DESC").list();
        session.getTransaction().commit();
        
        return list;
	}
	

}
